//
//  TT_PhonaCallManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/3.
//  Copyright © 2020 王洋. All rights reserved.
//

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
//
//#import <CallKit/CallKit.h>



#import "TT_PhonaCallManager.h"

@interface TT_PhonaCallManager ()

@property (nonatomic, strong) CTCallCenter *callCenter;

//@property(nonatomic,strong)CXCallObserver *callObserver;

@property (nonatomic, copy) NSString *privateNum;

@end

@implementation TT_PhonaCallManager

+ (TT_PhonaCallManager *)manager {
    
    static TT_PhonaCallManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_PhonaCallManager new];
    });
    return shareInstance;
}

- (void)callWithStoreId:(NSString *)storeId {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:storeId forKey:@"sellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StorePhone];
    
//    [SVProgressHUD showWithStatus:@"正在获取隐私号码"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
//        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSString *privateNum = [resultDic valueForKey:@"privateNum"];
                weakSelf.privateNum = privateNum;
                [Utilities callPhone:privateNum];
                [weakSelf callPhoneBack];
            }
        } else {
            
            [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
//        [SVProgressHUD dismiss];
    }];
}

- (void)callPhoneBack {

    self.callCenter = [[CTCallCenter alloc] init];
    WEAKSELF
    [self.callCenter setCallEventHandler:^(CTCall * _Nonnull call) {
        if ([[call callState] isEqual:CTCallStateDisconnected]) {
            
            NSLog(@"挂电话");
            if (weakSelf.privateNum.length > 0) {
                [weakSelf request_unBindPhone];
            }
        }
    }];
    
}

- (void)request_unBindPhone {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.privateNum forKey:@"privateNum"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ElievePhone];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
    }];
    
}


@end
